package com.yangquan.ayuan.unsafeInventory;

public class UnsafeInventory {
    private int stock = 1;

    // 加锁，保证 {判断库存} 和 {扣减库存} 的原子性
    public synchronized boolean deductStock(int quantity) {
        if (stock >= quantity) {
            // 模拟业务处理耗时
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
            stock -= quantity;
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(new UnsafeInventory().deductStock(1));
    }
}